import {createStore} from 'vuex'
import router from '@/router'

export default createStore({
	state: {
		token:null,
		menuList:null,
		userInfo:null,
		hasRoutes: false,
		perms:null,
		editableTabsValue:'/index',
		editableTabs:[
			{
				title:'首页',
				name:'/index'
			}
		]
	},
	getters: {
		GET_TOKEN: state => {
			return state.token || sessionStorage.getItem("token")
		},
		GET_USERINFO: state => {
			return JSON.parse(sessionStorage.getItem("userInfo"))
		},
		GET_MENULIST: state => {
			return state.menuList || JSON.parse(sessionStorage.getItem("menuList"));
		},
		GET_PERMS: state => {
			return state.perms || JSON.parse(sessionStorage.getItem("perms"));
		},
	},
	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token
			sessionStorage.setItem("token", token);
		},
		SET_USERINFO: (state, userInfo) => {
			state.userInfo = userInfo
			sessionStorage.setItem("userInfo", JSON.stringify(userInfo));
		},
		SET_MENULIST: (state, menuList) => {
			state.menuList = menuList
			sessionStorage.setItem("menuList", JSON.stringify(menuList));
		},
		SET_PERMS: (state, perms) => {
			state.perms = perms
			sessionStorage.setItem("perms", JSON.stringify(perms));
		},
		SET_ROUTES_STATE: (state, hasRoutes) => {
			state.hasRoutes = hasRoutes
		},
		ADD_TABS:(state,tab)=>{
			if(state.editableTabs.findIndex(e=>e.name===tab.path)===-1){
				state.editableTabsValue = tab.path
				state.editableTabs.push({
					title:tab.name,
					name:tab.path
				})
			}
			state.editableTabsValue=tab.path
		},
		INDEX_TAB:(state)=>{
			state.editableTabsValue='/index'
		},
		RESET_TABS:(state)=>{
			state.editableTabsValue='/index';
			state.editableTabs=[
				{
					title:'首页',
					name:'/index'
				}
			]
		},
	},
	actions: {
		logout() {
			window.sessionStorage.clear()
			router.replace('/login')
		}
	},
	modules: {}
})
